package com.brspace.qualitycontrol.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brspace.qualitycontrol.entity.Leaveing;
import com.brspace.qualitycontrol.entity.User;
import com.brspace.qualitycontrol.mapper.UserMapper;
import com.brspace.qualitycontrol.service.UserService;
import com.brspace.qualitycontrol.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户控制层
 */
@RestController
@RequestMapping("/admin/user")
@Api(description = "用户模块-管理接口")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private UserMapper userMapper;

    /**
     *用户登录
     *@author Hugo
     *@date 2021-04-16 17:53
     **/
    @PostMapping("/login")
    public R login(User user){
        return R.ok().data("list",userService.login(user));
    }

    /**
     * 用户注册
     *@author Hugo
     *@date 2021-04-15 15:41
     **/
    @PostMapping("save")
    public R saveUser(User user){
        userService.saveInfo(user);
        return R.ok();
    }

    /**
     *获取部门下的人员
     *@author Hugo
     *@date 2021-04-22 19:04
     **/
    @PostMapping("getPeople/{deptId}")
    public R getPeople(@PathVariable String deptId){
        return R.ok().data("list",userMapper.selectList(new QueryWrapper<User>().eq("dept_id",deptId)));
    }
    
    /**
     *用户修改
     *@author Hugo
     *@date 2021-04-15 15:55
     **/
    @PostMapping("update")
    public R update(User user){
        userService.updateById(user);
        return R.ok();
    }
    /**
     *获取用户信息
     *@author Hugo
     *@date 2021-04-15 15:46
     **/
    @PostMapping("/getInfo/{id}")
    public R getInfo(@PathVariable String id){
        return R.ok().data("list",userService.getById(id));
    }

    /**
     *删除用户
     *@author Hugo
     *@date 2021-04-15 15:52
     **/
    @PostMapping("delete/{id}")
    public R delete(@PathVariable Integer id){
        userService.removeById(id);
        return R.ok();
    }

    @ApiOperation("获取用户列表")
    @PostMapping("list/{page}/{limit}")
    public R list(@ApiParam(value = "当前页码",required = true) @PathVariable Long page,
                  @ApiParam(value = "每页多少数据", required = true) @PathVariable Long limit,
                  User user){
        Page<User> pageParam = new Page<>(page, limit);
        IPage<User> pageModel = userService.selectPage(pageParam, user);
        List<User> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("人事代办")
    @PostMapping("findList/{page}/{limit}/{id}")
    public R findList(@ApiParam(value = "当前页码",required = true) @PathVariable int page,
                      @ApiParam(value = "每页多少数据", required = true) @PathVariable int limit,
            @ApiParam(value = "id",required = true) @PathVariable String id){
        return userService.findAll(page,limit,id);
    }

}
